package sessionBeanTest;

import interfaceBeanRemote.sessionStartUpRemote;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

import entityBean.Participation;
import entityBean.Startup;

public class StartupTest extends TestCase{

	Context ctx1;
	private sessionStartUpRemote startupBean;

	

	@BeforeClass
	public void setUp() throws Exception {

		ctx1= new InitialContext();

		ctx1.addToEnvironment(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
		ctx1.addToEnvironment(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
		ctx1.addToEnvironment(InitialContext.PROVIDER_URL,"jnp://localhost:1099");

		startupBean = (sessionStartUpRemote) ctx1.lookup("sessionStartUp/remote");
		
	}
	
	@After
	public void tearDown() throws Exception {
		
	}
	
	@Test 
	public void testAddStartUp(){
		
		Startup startup6 = new Startup();
		startup6.setNom("Startup");
		startupBean.addStartUp(startup6);

	}
	
	@Test 
	public void testGetStartUpListWithParticipations(){
		
		Startup startupList1 = new Startup();
		startupList1.setNom("startup1");
		Participation participation1 = new Participation();
		List <Participation> participations1 = new ArrayList<Participation>();
		participations1.add(participation1);
		participation1.setStartup(startupList1);
		startupList1.setParticipations(participations1);
		
		startupBean.addStartUp(startupList1);
		
		Startup startupList2 = new Startup();
		startupList2.setNom("startup2");
		Participation participation2 = new Participation();
		List <Participation> participations2 = new ArrayList<Participation>();
		participations2.add(participation2);
		startupList2.setParticipations(participations2);
		participation2.setStartup(startupList2);
		startupBean.addStartUp(startupList2);
		
		
		List<Startup> startups = startupBean.getStartUpListWithParticipations();
		System.out.println(startups.get(0).getNom());
		assertEquals("startup1", startups.get(0).getNom());
		assertEquals(1, startups.get(0).getParticipations().get(0).getIdParticipation());
		//startupBean.removeStartUpById(1);
		
		
	}
	
	@Test 
	public void testFindStartUp(){
		
		
		Startup startup1 = startupBean.findStartUp(4);
		assertEquals("startup1", startup1.getNom());
		
	}
	
	
	/* A modifier ou utiliser WithParticipations  
	@Test 
	public void testGetStartUpList(){
		
		List<Startup> startups = startupBean.getStartUpList();
		assertEquals("startup1", startups.get(0).getNom());
		assertEquals("startup2", startups.get(1).getNom());
	}
	
	*/
	
	@Test 
	public void testUpdateStartUp(){
		
		Startup startup2 = startupBean.findStartUp(1);
		startup2.setNom("StartupUpdate");
		startupBean.updateStartUp(startup2);
		
		Startup startup3 = startupBean.findStartUp(1);
		assertEquals("StartupUpdate", startup3.getNom());
		
		startupBean.removeStartUpById2(1);
		startupBean.removeStartUpById2(2);
		
	}
	
	
	
	@Test 
	public void testRemoveStartUp(){
		
		Startup startup7 = new Startup();
		startup7.setNom("Startup");
		startupBean.addStartUp(startup7);
		startupBean.removeStartUp(startup7);

	}
}
